<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Attractions\Commerce\Repository\Commerce_qualification;

use Plugin\Attractions\Commerce\Model\Commerce_qualification\CommerceQualification;
use Hyperf\Database\Model\Builder;
use App\Repository\IRepository;
use Hyperf\Collection\Arr;

/**
 * 商户资质信息 Repository类
 */
class CommerceQualificationRepository extends IRepository
{
   public function __construct(
        protected readonly CommerceQualification $model
    ) {}

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        
        // 主键ID
        if (isset($params['id']) && !empty($params['id'])) {
            $query->where('id', '=', $params['id']);
        }

        // 商户ID
        if (isset($params['merchant_id']) && !empty($params['merchant_id'])) {
            $query->where('merchant_id', '=', $params['merchant_id']);
        }

        // 资质类型：1-食品经营许可证，2-卫生许可证，3-消防许可证，4-其他证书
        if (isset($params['qualification_type']) && !empty($params['qualification_type'])) {
            $query->where('qualification_type', '=', $params['qualification_type']);
        }

        // 资质名称
        if (isset($params['qualification_name']) && !empty($params['qualification_name'])) {
            $query->where('qualification_name', 'like', '%'.$params['qualification_name'].'%');
        }

        // 资质证书号
        if (isset($params['qualification_id']) && !empty($params['qualification_id'])) {
            $query->where('qualification_id', 'like', '%'.$params['qualification_id'].'%');
        }

        // 发证机关
        if (isset($params['issuing_authority']) && !empty($params['issuing_authority'])) {
            $query->where('issuing_authority', 'like', '%'.$params['issuing_authority'].'%');
        }

        // 发证日期
        if (isset($params['issue_date']) && !empty($params['issue_date']) && is_array($params['issue_date']) && count($params['issue_date']) == 2) {
            $query->whereBetween(
                'issue_date',
                [ $params['issue_date'][0], $params['issue_date'][1] ]
            );
        }

        // 有效期至
        if (isset($params['expiry_date']) && !empty($params['expiry_date']) && is_array($params['expiry_date']) && count($params['expiry_date']) == 2) {
            $query->whereBetween(
                'expiry_date',
                [ $params['expiry_date'][0], $params['expiry_date'][1] ]
            );
        }

        // 资质文件路径
        if (isset($params['qualification_file']) && !empty($params['qualification_file'])) {
            $query->where('qualification_file', '=', $params['qualification_file']);
        }

        // 备注
        if (isset($params['remark']) && !empty($params['remark'])) {
            $query->where('remark', 'like', '%'.$params['remark'].'%');
        }

        return $query;
    }
}